package com.hua.gulimall.product.controller;

import com.hua.common.utils.PageUtils;
import com.hua.common.utils.R;
import com.hua.gulimall.product.entity.AttrEntity;
import com.hua.gulimall.product.entity.AttrGroupEntity;
import com.hua.gulimall.product.service.AttrAttrgroupRelationService;
import com.hua.gulimall.product.service.AttrGroupService;
import com.hua.gulimall.product.service.AttrService;
import com.hua.gulimall.product.service.CategoryService;
import com.hua.gulimall.product.vo.AttGrouprRelationVo;
import com.hua.gulimall.product.vo.AttrGroupWithAttrVo;
import org.mybatis.logging.Logger;

import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author ahua
 * @email 123456@163.com
 * @date 2021-11-15 19:20:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    private final Logger logger = LoggerFactory.getLogger(AttrGroupController.class);

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttGrouprRelationVo> relationVos){

        attrService.deleteRelation(relationVos);


        return  R.ok();

    }

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
       List<AttrGroupWithAttrVo> vos =  attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


    /**
     * 获取指定分组关联的所有属性
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
       List<AttrEntity> attrEntities = attrService.getAttrRelation(attrGroupId);
       return R.ok().put("data",attrEntities);
    }

    /**
     *
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttGrouprRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrgroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoAttrRelation(params,attrGroupId);
        return R.ok().put("page",page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){

		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCateLogPath(catelogId);

        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
